package com.atguigu.gmall.order.api;

import com.atguigu.gmall.common.result.Result;
import com.atguigu.gmall.common.result.ResultCodeEnum;
import com.atguigu.gmall.common.util.AuthContextHolder;
import com.atguigu.gmall.order.model.OrderInfo;
import com.atguigu.gmall.order.service.OrderInfoService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang.StringUtils;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

/**
 * @author: atguigu
 * @create: 2023-09-15 10:33
 */
@RestController
@RequestMapping("/api/order")
public class OrderApiController {

    @Autowired
    private OrderInfoService orderInfoService;

    /**
     * 获取当前登录用户订单确认页相关数据汇总
     *
     * @return
     */
    @GetMapping("/auth/trade")
    public Result orderTradeData(HttpServletRequest request) {
        //1.获取当前登录用户ID
        String userId = AuthContextHolder.getUserId(request);
        if (StringUtils.isBlank(userId)) {
            throw new RuntimeException(ResultCodeEnum.LOGIN_AUTH.getMessage());
        }
        //2.汇总订单确认页面相关数据
        Map<String, Object> mapResult = orderInfoService.orderTradeData(userId);
        return Result.ok(mapResult);
    }


    /**
     * 根据订单ID查询订单及订单明细
     *
     * @param orderId
     * @return
     */
    @ApiOperation("根据订单ID查询订单及订单明细")
    @GetMapping("/inner/getOrderInfo/{orderId}")
    public OrderInfo getOrderInfo(@PathVariable("orderId") Long orderId) {
        return orderInfoService.getOrderInfo(orderId);
    }


    /**
     * 提交秒杀订单
     *
     * @param orderInfo
     * @return
     */
    @ApiOperation("提交秒杀订单")
    @PostMapping("/inner/seckill/submitOrder")
    public Long submitSeckillOrder(@RequestBody OrderInfo orderInfo) {
        return orderInfoService.submitSeckillOrder(orderInfo);
    }
}
